package com.example.pingze.controller;

import com.example.pingze.entity.Ranking;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.RankingService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ranking")
@CrossOrigin(origins = "*")
public class RankingController {
    @Autowired
    private RankingService service;

    // 通过关键字获取用户发表,count排序
    @PostMapping("/getRankings")
    public Result<List<Ranking>> getRankings(@RequestParam String keyword){
        if (keyword == null) {
            throw new PingZeException("关键字为空");
        }
        List<Ranking> rankings = service.findRankingsByKeyword(keyword);
        if (rankings != null) {
            return ResultVOUtils.result(rankings);
        }else {
            throw new PingZeException("ranking_该关键字没有用户发表哦");
        }
    }
    // 通过account和关键字，查询用户发表数据
    @PostMapping("/getCount")
    public Result<Ranking> getCompeteCount(@RequestParam String account,
                                           @RequestParam String keyword){
        if (account == null){
            throw new PingZeException("compete_account为空");
        }
        if (keyword == null){
            throw new PingZeException("compete_keyword为空");
        }
        Ranking ranking = service.findRankingByAccountAndKeyword(account, keyword);
        if (ranking != null) {
            return ResultVOUtils.result(ranking);
        } else {
            throw new PingZeException("compete_没有找到相关信息");
        }
    }
}
